package com.mall.group_booking.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.common.response.CommonResponse;
import com.mall.constant.enums.BookingRootStatus;
import com.mall.constant.enums.CommonStatus;
import com.mall.constant.enums.ConstantStatus;
import com.mall.constant.strings.ErrorMsg;
import com.mall.constant.strings.IntStant;
import com.mall.constant.strings.MapMsg;
import com.mall.entity.groupbooking.GbActivity;
import com.mall.entity.groupbooking.GbItem;
import com.mall.entity.groupbooking.GbRoot;
import com.mall.entity.groupbooking.GbRoot;
import com.mall.group_booking.entity.GbItemVo;
import com.mall.group_booking.entity.GbRecordVo;
import com.mall.group_booking.mapper.BookingRootMapper;
import com.mall.group_booking.service.BookingRootService;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author White
 * @version 1.0
 * @email 616769721@qq.com
 * @date 2021/7/29 20:47
 */
@Service
public class BookingRootServiceImpl implements BookingRootService {

    @Autowired
    private Redisson redisson;

    @Autowired
    private BookingRootMapper bookingRootMapper;


    /**
     * wen
     * 查询拼团列表
     *
     * @param pageNum  页面num
     * @param pageSize 页面大小
     * @param root     条件
     * @return {@link PageInfo<GbRoot>}
     */
    @Override
    public PageInfo<GbRoot> findPage(int pageNum, int pageSize, GbRoot root) {
        long zero = System.currentTimeMillis()/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset();

        List<GbRoot> list = bookingRootMapper.findPage(root);

        if (root != null) {
            if (root.getTime() == IntStant.TWO) {
                list = list.stream().filter(e -> {
                    System.out.println(e.getCreateTime().getTime());
                    return e.getCreateTime().getTime() >= zero && e.getCreateTime().getTime() <= zero + IntStant.ONE_DAY;
                }).collect(Collectors.toList());
            }
            if (root.getTime() == IntStant.THREE) {
                list = list.stream().filter(e -> {
                    return e.getCreateTime().getTime() >= zero -IntStant.ONE_DAY && e.getCreateTime().getTime() <= zero;
                }).collect(Collectors.toList());
            }
            if (root.getTime() == IntStant.FORE) {
                list = list.stream().filter(e -> {
                    return e.getCreateTime().getTime() >= zero -IntStant.ONE_DAY * 7 && e.getCreateTime().getTime() <= zero;
                }).collect(Collectors.toList());
            }
            if (root.getTime() == IntStant.FIVE) {
                list = list.stream().filter(e -> {
                    return e.getCreateTime().getTime() >= zero -IntStant.ONE_DAY * 30 && e.getCreateTime().getTime() <= zero;
                }).collect(Collectors.toList());
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<GbRoot> gbRootPageInfo = new PageInfo<>(list);
        return gbRootPageInfo;
    }

    @Override
    public GbRoot doBookingRoot(GbItemVo gbItemVo,Long uid){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        GbRoot gbRoot = new GbRoot();
        gbRoot.setUid(uid);
        gbRoot.setCreateTime(calendar.getTime());
        //当前时间加拼团时效
        calendar.add(Calendar.HOUR,gbItemVo.getTimeLimit());
        gbRoot.setTime(gbItemVo.getTimeLimit());
        gbRoot.setEndTime(calendar.getTime());
        gbRoot.setActivityId(gbItemVo.getActivityId());
        gbRoot.setSpecItemId(gbItemVo.getSpecItemId());
        gbRoot.setStatus(BookingRootStatus.NON_PAYMENT.getCode());
        gbRoot.setJoinCount(1);
        bookingRootMapper.createBookingRoot(gbRoot);
        return gbRoot;
    }

    @Override
    public GbRoot findById(Integer rootId) {
        return bookingRootMapper.findById(rootId);
    }

    /**
     * 查询正在进行的拼团活动
     * @param uid
     * @return
     */
    @Override
    public List<GbRoot> findBookingRoot(Long uid) {
        return bookingRootMapper.findBookingRoot(uid);
    }

    @Override
    public void updateStatusById(Integer id, String status) {
        bookingRootMapper.updateStatusById(id,status);
    }

    @Override
    public void updateBookingRoot(GbRoot gbRoot) {
        bookingRootMapper.updateBookingRoot(gbRoot);
    }
}
